﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using ARR.Entities;
using ARR.UI.ViewModels;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for PlanogramsMasterView.xaml
    /// </summary>
    public partial class PlanogramsMasterView : UserControl
    {
        public PlanogramsMasterView()
        {
            InitializeComponent();

            PlanogramsGV.PreparingCellForEdit += new EventHandler<Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs>(PlanogramsGV_PreparingCellForEdit);
            PlanogramsGV.RowEditEnded += new EventHandler<GridViewRowEditEndedEventArgs>(PlanogramsGV_RowEditEnded);
        }

        void PlanogramsGV_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            if (e.Row != null && e.Row.Item != null && e.Row.Item is Planogram)
            {
                if (((Planogram)e.Row.Item).PlanogramType != PlanogramType.Mixed)
                {
                    ((Planogram)e.Row.Item).ListingPlanogram = null;
                    ((Planogram)e.Row.Item).ListingPlanogramId = null;
                }
            }

            PlanogramsGV.Items.Refresh();

        }

        void PlanogramsGV_PreparingCellForEdit(object sender, Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "ListingPlanogram")
            {
                RadComboBox combo = (RadComboBox)e.EditingElement;
                Planogram p = (Planogram)e.Row.Item;

                if (p.PlanogramType == PlanogramType.Mixed)
                {
                    PlanogramsMasterViewModel vm = this.DataContext as PlanogramsMasterViewModel;

                    combo.ItemsSource = vm.Planograms.Where(x => x.PlanogramType == PlanogramType.Listing && x.Id != p.Id);
                }
            }
        }
    }
}
